package com.happy.cat.project.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.*;
import java.util.stream.Collectors;

import com.happy.cat.common.utils.SecurityUtils;
import com.happy.cat.common.utils.StringUtils;
import com.happy.cat.project.system.domain.SysUser;
import com.happy.cat.project.system.service.ISysUserService;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.happy.cat.framework.aspectj.lang.annotation.Log;
import com.happy.cat.framework.aspectj.lang.enums.BusinessType;
import com.happy.cat.project.system.domain.CatChannel;
import com.happy.cat.project.system.service.ICatChannelService;
import com.happy.cat.framework.web.controller.BaseController;
import com.happy.cat.framework.web.domain.AjaxResult;
import com.happy.cat.common.utils.poi.ExcelUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.happy.cat.framework.web.page.TableDataInfo;

/**
 * 渠道管理Controller
 *
 * @author verne
 * @date 2020-08-22
 */
@AllArgsConstructor
@RestController
@RequestMapping("/system/channel" )
@Api("渠道管理 API")
public class CatChannelController extends BaseController {

    private final ICatChannelService iCatChannelService;

    private final ISysUserService sysUserService;

    /**
     * 查询渠道管理列表
     */
    @ApiOperation(value = "查询渠道管理列表",notes = "查询渠道管理列表",response = TableDataInfo.class)
    @PreAuthorize("@ss.hasPermi('system:catChannel:list')")
    @GetMapping("/list")
    public TableDataInfo list(CatChannel catChannel)
    {
        startPage();
        LambdaQueryWrapper<CatChannel> lqw = new LambdaQueryWrapper<CatChannel>();
        if (catChannel.getSysUserId() != null){
            lqw.eq(CatChannel::getSysUserId ,catChannel.getSysUserId());
        }
        if (catChannel.getChannelId() != null){
            lqw.eq(CatChannel::getChannelId ,catChannel.getChannelId());
        }
        if (catChannel.getParentId() != null){
            lqw.eq(CatChannel::getParentId ,catChannel.getParentId());
        }
        if (StringUtils.isNotBlank(catChannel.getParentBusiness())){
            lqw.eq(CatChannel::getParentBusiness ,catChannel.getParentBusiness());
        }
        if (StringUtils.isNotBlank(catChannel.getName())){
            lqw.like(CatChannel::getName ,catChannel.getName());
        }
        if (StringUtils.isNotBlank(catChannel.getContactPerson())){
            lqw.eq(CatChannel::getContactPerson ,catChannel.getContactPerson());
        }
        if (StringUtils.isNotBlank(catChannel.getContactPhone())){
            lqw.eq(CatChannel::getContactPhone ,catChannel.getContactPhone());
        }
        if (StringUtils.isNotBlank(catChannel.getWechat())){
            lqw.eq(CatChannel::getWechat ,catChannel.getWechat());
        }
        if (StringUtils.isNotBlank(catChannel.getAliName())){
            lqw.like(CatChannel::getAliName ,catChannel.getAliName());
        }
        if (StringUtils.isNotBlank(catChannel.getAliAccount())){
            lqw.eq(CatChannel::getAliAccount ,catChannel.getAliAccount());
        }
        if (StringUtils.isNotBlank(catChannel.getProveNumber())){
            lqw.eq(CatChannel::getProveNumber ,catChannel.getProveNumber());
        }
        if (catChannel.getSettleUnitPrice() != null){
            lqw.eq(CatChannel::getSettleUnitPrice ,catChannel.getSettleUnitPrice());
        }
        if (catChannel.getChatroomCommissionRatio() != null){
            lqw.eq(CatChannel::getChatroomCommissionRatio ,catChannel.getChatroomCommissionRatio());
        }
        if (catChannel.getOtherCommissionRatio() != null){
            lqw.eq(CatChannel::getOtherCommissionRatio ,catChannel.getOtherCommissionRatio());
        }
        if (catChannel.getMaxRegister() != null){
            lqw.eq(CatChannel::getMaxRegister ,catChannel.getMaxRegister());
        }
        if (catChannel.getCreateDate() != null){
            lqw.eq(CatChannel::getCreateDate ,catChannel.getCreateDate());
        }
        if (catChannel.getStatus() != null){
            lqw.eq(CatChannel::getStatus ,catChannel.getStatus());
        }
        if (catChannel.getApplyStatus() != null){
            lqw.eq(CatChannel::getApplyStatus ,catChannel.getApplyStatus());
        }
        if (StringUtils.isNotBlank(catChannel.getApplyText())){
            lqw.eq(CatChannel::getApplyText ,catChannel.getApplyText());
        }
        if (catChannel.getApplyTime() != null){
            lqw.eq(CatChannel::getApplyTime ,catChannel.getApplyTime());
        }
        if (StringUtils.isNotBlank(catChannel.getApplyBy())){
            lqw.eq(CatChannel::getApplyBy ,catChannel.getApplyBy());
        }
        List<CatChannel> list = iCatChannelService.list(lqw);

        if(list.size()<=0){
            return getDataTable(list);
        }

        Set<Long> uids = list.stream().map(CatChannel::getSysUserId).collect(Collectors.toSet());
        Map<Long, SysUser> userMap = sysUserService.listByIds(uids).stream().collect(Collectors.toMap(SysUser::getUserId, e -> e));

        list.forEach(e -> {
            if(userMap.get(e.getSysUserId())!=null){
                e.setManagerAccount(userMap.get(e.getSysUserId()).getUserName());
                e.setManagerName(userMap.get(e.getSysUserId()).getNickName());
            }
        });
        return getDataTable(list);
    }

    /**
     * 导出渠道管理列表
     */
    @ApiOperation(value = "导出渠道管理列表",notes = "导出渠道管理列表",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:catChannel:export')" )
    @Log(title = "渠道管理" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(CatChannel catChannel) {
        LambdaQueryWrapper<CatChannel> lqw = new LambdaQueryWrapper<CatChannel>(catChannel);
        List<CatChannel> list = iCatChannelService.list(lqw);
        ExcelUtil<CatChannel> util = new ExcelUtil<CatChannel>(CatChannel. class);
        return util.exportExcel(list, "catChannel" );
    }

    /**
     * 获取渠道管理详细信息
     */
    @ApiOperation(value = "获取渠道管理详细信息",notes = "获取渠道管理详细信息",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:catChannel:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iCatChannelService.getById(id));
    }

    /**
     * 新增渠道管理
     */
    @ApiOperation(value = "新增渠道管理",notes = "新增渠道管理",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:catChannel:add')" )
    @Log(title = "渠道管理" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CatChannel catChannel) {
        catChannel.setCreateDate(new Date());
        catChannel.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iCatChannelService.save(catChannel) ? 1 : 0);
    }

    /**
     * 修改渠道管理
     */
    @ApiOperation(value = "修改渠道管理",notes = "修改渠道管理",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:catChannel:edit')" )
    @Log(title = "渠道管理" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CatChannel catChannel) {
        return toAjax(iCatChannelService.updateById(catChannel) ? 1 : 0);
    }

    /**
     * 删除渠道管理
     */
    @ApiOperation(value = "删除渠道管理",notes = "删除渠道管理",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:catChannel:remove')" )
    @Log(title = "渠道管理" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}/{status}" )
    public AjaxResult remove(@PathVariable Long[] ids, @PathVariable Integer status) {
        return toAjax(iCatChannelService.banStatusByIds(Arrays.asList(ids), status) ? 1 : 0);
//        return toAjax(iCatChannelService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
